﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Achievement.Common;
using System.Configuration;

namespace Achievement.Web.Common
{
    public static class CommonTools
    {
        public static string OperateAlter(string sText)
        {
            return "<script>alert('" + sText + "！');alert('window.location.href');</script>";
        }


        public static string OperateAlter(string sText, string sUrl)
        {
            return "<script>alert('" + sText + "！');window.location.href='" + sUrl + "';</script>";
        }

        public static string UploadFile(HttpPostedFileBase file, out string fileExtension)
        {
            string filePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["SavePath"]);
            fileExtension = System.IO.Path.GetExtension(file.FileName);
            string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + (new Random()).Next(9999).ToString();
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            string saveName = "File\\" + fileName + fileExtension;
            file.SaveAs(filePath + saveName);
            //切记，使用pdf2swf.exe 打开的文件名之间不能有空格，否则会失败
            return "/file/" + fileName + fileExtension;
        }

        public static string UploadOffice2Pdf(string fileName)
        {
            string filePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["SavePath"]);
            fileName = fileName.Substring(fileName.LastIndexOf("/") + 1);
            string oFileName = fileName;
            fileName = fileName.Substring(0, fileName.LastIndexOf("."));
            string pdfFileName = "file\\" + fileName + ".pdf";
            ATools.ExportPdf(filePath + "file\\" + oFileName, filePath + pdfFileName);
            return "file/" + fileName + ".pdf";
        }

        public static string UploadPdf2Swf(string pdfFileName)
        {
            string cmdStr = HttpContext.Current.Server.MapPath("~/Tools/pdf2swf.exe");
            string savePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["SavePath"]);
            //将PDF文件转换成SWF格式文件
            //要转换的pdf文件路径
            string sourcePath = @"""" + savePath + pdfFileName + @"""";

            //转换之后swf文件存放的目标路径
            pdfFileName = pdfFileName.Substring(pdfFileName.LastIndexOf("/") + 1);
            string swfName = "/swf/" + pdfFileName.Substring(0, pdfFileName.LastIndexOf(".")) + ".swf";
            string targetPath = @"""" + savePath + swfName + @"""";
            //@"""" 四个双引号得到一个双引号，如果你所存放的文件所在文件夹名有空格的话，要在文件名的路径前后加上双引号，才能够成功
            // -t 源文件的路径
            // -s 参数化（也就是为pdf2swf.exe 执行添加一些窗外的参数(可省略)）
            string argsStr = "  -t " + sourcePath + " -s flashversion=9 -o " + targetPath;

            //执行pdf到swf的转换
            if (!Directory.Exists(savePath + "/swf/"))
            {
                Directory.CreateDirectory(savePath + "/swf/");
            }
            ATools.ExcutedCmd(cmdStr, argsStr);
            if (!File.Exists(savePath + swfName))
            {
                ATools.ExcutedCmd(cmdStr, argsStr + " -f -T 9 -G -s poly2bitmap");
            }
            return swfName;
        }
    }
}